package net.alan.common.utils;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/12/18
 */

import difflib.Delta;
import difflib.DiffRowGenerator;
import difflib.DiffUtils;
import difflib.Patch;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompareUtil {
    public static String wrapHtml(String content) {
        String html = "<div></div>" + content + "<div></div>";
        return html;
    }

    private static List<String> buildHtml(String content) {
        List<String> list = new ArrayList<String>();
        Document document = Jsoup.parse(content);
        Elements eles = document.getAllElements();
        for (int i = 0; i < eles.size(); i++) {
            Element ele = eles.get(i);
            list.add(ele.text());
        }
        return list;
    }

    public static String compare(String sb1, String sb2) {
        List<String> rsList = new ArrayList<String>();
        List list1 = buildHtml(sb1);
        List list2 = buildHtml(sb2);
        Document document = Jsoup.parse(sb2);
        Elements eles = document.getAllElements();
        Map<String, Element> eleMap = new HashMap();
        for (int i = 0; i < eles.size(); i++) {
            Element ele = eles.get(i);
            eleMap.put(ele.text(), ele);
        }
        Patch patch = DiffUtils.diff(list1, list2);
        for (Delta d : patch.getDeltas()) {
            List list = d.getRevised().getLines();
            for (Object s : list) {
                Element ele = eleMap.get(s.toString());
                if (ele != null) {
                    ele.addClass("changes");
                }
            }
        }
        return document.html();
    }

    @Test
    public void testCompare() throws IOException {
        List<String> original = FileUtils.readLines(new File("D:\\Android Studio\\build.txt"), "gbk");
        List<String> revised = FileUtils.readLines(new File("D:\\Android Studio\\NOTICE.txt"), "gbk");

        Patch patch = DiffUtils.diff(original, revised);
        for (Delta d : patch.getDeltas()) {
            System.out.println(d.getRevised().getLines());
        }
        DiffRowGenerator.Builder builder = new DiffRowGenerator.Builder();
        builder.showInlineDiffs(true);
        DiffRowGenerator generator = builder.build();
        /*for (Delta delta : patch.getDeltas()) {
            List<DiffRow> generateDiffRows = generator.generateDiffRows((List<String>) delta.getOriginal().getLines(), (List<String>) delta
                    .getRevised().getLines());
            int leftPos = delta.getOriginal().getPosition();
            int rightPos = delta.getRevised().getPosition();
            for (DiffRow row : generateDiffRows) {
                Tag tag = row.getTag();
                if (tag == Tag.INSERT) {
                    System.out.println("Insert: ");
                    System.out.println("new-> " + row.getNewLine());
                    System.out.println("");
                } else if (tag == Tag.CHANGE) {
                    System.out.println("change: ");
                    System.out.println("old-> " + row.getOldLine());
                    System.out.println("new-> " + row.getNewLine());
                    System.out.println("");
                } else if (tag == Tag.DELETE) {
                    System.out.println("delete: ");
                    System.out.println("old-> " + row.getOldLine());
                    System.out.println("");
                } else if (tag == Tag.EQUAL) {
                    System.out.println("equal: ");
                    System.out.println("old-> " + row.getOldLine());
                    System.out.println("new-> " + row.getNewLine());
                    System.out.println("");
                } else {
                    throw new IllegalStateException("Unknown pattern tag: " + tag);
                }
            }
        }*/
    }
}